package com.bytz.modules.cms.domain.impl.order;

import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.order.model.OrderModel;
import com.bytz.modules.cms.productLine.entity.ProductLine;
import com.bytz.modules.cms.user.entity.CmsSysUser;
import com.bytz.modules.system.model.CodeGenerateRo;
import com.bytz.modules.system.service.ICodeGeneratorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @program: cms-backend
 *
 * @description:
 *
 * @author: KyleWang
 *
 * @create: 2022-11-08 11:21
 **/

public class MallOrderNumberGeneratorProcessor extends MallOrderAbstractProcessor {
    public MallOrderNumberGeneratorProcessor(int sequence) {
        super(sequence);
    }


    public static final DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyMMdd");
    public static final String format = "%s%02d";


    private ICodeGeneratorService codeGeneratorService;

    @Autowired
    public void setCodeGeneratorService(ICodeGeneratorService codeGeneratorService) {
        this.codeGeneratorService = codeGeneratorService;
    }

    @Override
    @Transactional
    public void doProcess(MallOrderProcessContext context) {
        if (!context.getSubmit()) {
            return;
        }
        StringBuilder key = new StringBuilder();
        OrderModel order = context.getOrder();
        CmsSysUser cmsSysUser = context.getSeller();
        if (cmsSysUser == null) {
            throw new BytzBootException("找不到销售人员" + order.getSalesName());
        }
        //产品线
        ProductLine productLine = context.getSellerProductLine();
        if (productLine == null) {
            throw new BytzBootException("找不到销售人员对应产品线");
        }
        //合同首字母
        if (StringUtils.isNotBlank(productLine.getContractInitailLetter())) {
            key.append(productLine.getContractInitailLetter());
        } else {
            throw new BytzBootException(String.format("产品线[%s]合同首字母未定义", productLine.getProductLineName()));
        }

        //区域码
        if (StringUtils.equals(productLine.getProductLineCode(), "IV")) {
            if (StringUtils.isNotBlank(cmsSysUser.getArea())) {

                key.append(cmsSysUser.getArea());
            } else {
                throw new BytzBootException("找不到销售区域码");
            }
        }

        //人员简称
        if (StringUtils.isNotBlank(cmsSysUser.getShortName())) {
            key.append(cmsSysUser.getShortName());
        } else {
            throw new BytzBootException("找不到销售人员简称");
        }
        String yyMMdd = LocalDate.now().format(pattern);
        key.append(yyMMdd);
        String codeKey = key.toString();

        CodeGenerateRo generator = generator(codeKey);
        String suffix = null;

        if (StringUtils.equals(productLine.getProductLineCode(), "VTS")) {
            suffix = "PP";
        }

        Integer orderNum = Integer.valueOf(codeGeneratorService.getCode(generator));
        String contractNum=String.format(format, codeKey, orderNum);



        if (StringUtils.isNotBlank(suffix)) {
            contractNum += suffix;
        }
        order.setContractNumber(contractNum);
    }


    private CodeGenerateRo generator(String key) {
        CodeGenerateRo codeGenerateRo = new CodeGenerateRo();
        codeGenerateRo.setServiceName("orderContractNumber");
        codeGenerateRo.setRemark("订单合同编号生成");
        codeGenerateRo.setKeyName(key);
        return codeGenerateRo;
    }

}